package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsStudentMapper;
import cn.kai.ums.model.UmsGrade;
import cn.kai.ums.model.UmsStudent;
import cn.kai.ums.service.UmsGradeService;
import cn.kai.ums.service.UmsStudentService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/student")
@Api(tags = "学生控制类")
public class StudentController {

    @Autowired
    private UmsStudentService umsStudentService;

    @Autowired
    private UmsStudentMapper umsStudentMapper;

    @Autowired
    private UmsGradeService umsGradeService;


    @ApiOperation("根据学号，学生姓名，班级编号进行模糊查询")
    @GetMapping
    public JsonData getStuList( @RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likeStudent",required = false) String likeStudent){
        Page<UmsStudent> umsStudentPage = umsStudentMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsStudent>lambdaQuery()
                        .like(UmsStudent::getStudentNo,likeStudent)
                        .or()
                        .like(UmsStudent::getStudentName,likeStudent)
                        .or()
                        .like(UmsStudent::getStudentClassNo,likeStudent));
        return JsonData.buildSuccess(umsStudentPage);
    }

    @PostMapping
    public JsonData saveStudent(@RequestBody UmsStudent student){
        UmsStudent umsStudent = umsStudentService.getOneByno(student.getStudentNo());
        if(umsStudent==null){
            return JsonData.buildSuccess(umsStudentService.save(student));
        }else {
            return JsonData.buildError("学号不可重复");
        }
    }

    @PutMapping
    public JsonData updateStudent(@RequestBody UmsStudent student){
        return JsonData.buildSuccess(umsStudentService.updateById(student));
    }

    @DeleteMapping
    public JsonData delStudent(@RequestParam Integer stuid){
        return JsonData.buildSuccess(umsStudentService.removeById(stuid));
    }


    @PostMapping("/delBatchStudent")
    public JsonData delBatchStudent(@RequestBody List<Integer> stuids){
        return JsonData.buildSuccess(umsStudentService.removeByIds(stuids));
    }

    @ApiOperation("获取学生的必修课个人成绩")
    @GetMapping("/studentGrade")
    public JsonData studentGrade(@RequestParam String stuid){
        List<UmsGrade> umsGrades = umsGradeService.studentGrade(stuid);
        return JsonData.buildSuccess(umsGrades);
    }



    @ApiOperation("获取学生的选修课个人成绩")
    @GetMapping("/studentPubGrade")
    public JsonData studentPubGrade(@RequestParam String stuid){
        List<UmsGrade> umsGrades = umsGradeService.studentPubGrade(stuid);
        return JsonData.buildSuccess(umsGrades);
    }

    @GetMapping("/getOneStuByNo")
    public JsonData getOneStuByNo(@RequestParam String stno){
        return JsonData.buildSuccess(umsStudentService.getOneByno(stno));
    }

}
